package com.woniu.adopt_web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.adopt_web.entity.Pet;
import com.woniu.adopt_web.entity.PetAdopt;
import com.woniu.adopt_web.entity.PetCollection;
import com.woniu.adopt_web.entity.RescueStation;
import com.woniu.adopt_web.mapper.PetAdoptMapper;
import com.woniu.adopt_web.mapper.PetCollectionMapper;
import com.woniu.adopt_web.mapper.PetMapper;
import com.woniu.adopt_web.mapper.RescueStationMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.String;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class PetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetAdoptMapper petAdoptMapper;
    @Autowired
    private RescueStationMapper rescueStationMapper;
    @Autowired
    private PetCollectionMapper petCollectionMapper;
    
    public PageInfo<Pet> queryByPage(Integer now,Integer size,Integer category,Integer sex,String city){
        PageHelper.startPage(now,size);
        List<Pet> pets=petMapper.selectAll(category,sex,city);
        return new PageInfo<>(pets);
    }

    public Pet queryPetByKey(Integer id){
        Pet pet = petMapper.selectByPrimaryKey(id);
        pet.setViews(pet.getViews()+1);
        petMapper.updateByPrimaryKey(pet);
        return pet;
    }

    public PetAdopt findAdoptByUidAndPid(int uid,int pid){
        //应该查领养表 是否存在 且status！=0
        return petAdoptMapper.selectByUidAndPid(uid,pid);
    }

    public int addAdopt(int uid,int pid){
        PetAdopt adopt = new PetAdopt();
        adopt.setUserId(uid);
        adopt.setPetId(pid);
        adopt.setStatus(0);
        adopt.setInsertTime(new Date());
        return petAdoptMapper.insert(adopt);
    }

    public RescueStation findStationById(Integer id){
        return rescueStationMapper.selectByPrimaryKey(id);
    }


    public PetCollection findCollectionByUidAndPid(int uid,int pid){
        return petCollectionMapper.selectByUidAndPid(uid, pid);
    }

    public int addCollection(int uid,int pid){
        PetCollection collection = new PetCollection();
        collection.setUserId(uid);
        collection.setPetId(pid);
        collection.setInsertTime(new Date());
        return petCollectionMapper.insert(collection);
    }

    public List<String> getListCity(){
        List<RescueStation> rescueStations = rescueStationMapper.selectAll();
        ArrayList<String> listStrings = new ArrayList<>();
        for (RescueStation rescueStation : rescueStations) {
            String city=StringUtils.substringBeforeLast(rescueStation.getAddress(), "市");
            if(!listStrings.contains(city)) {
                listStrings.add(city);
            }
        }
        return listStrings;
    }

    public int removeCollection(int id){
        return petCollectionMapper.deleteByPrimaryKey(id);
    }
}
